package calendar.commands.events;

import calendar.commands.ClientCommand;
import calendar.model.CalendarModel;
import calendar.view.panels.CalendarGridPanel;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.http.HttpHeaders;
import com.google.api.services.calendar.model.Calendar;
import swing.Dialog;

public class AddCalendarUsingBatch extends ClientCommand {

    public AddCalendarUsingBatch(com.google.api.services.calendar.Calendar client) {
        super(client);
    }

    @Override
    protected void protectedExecute() throws Exception {
        BatchRequest batch = client.batch();

        // Create the callback.
        JsonBatchCallback<Calendar> callback = new JsonBatchCallback<Calendar>() {

            @Override
            public void onSuccess(Calendar calendar, HttpHeaders responseHeaders) {
                Dialog.popup(new CalendarGridPanel(calendar));
                CalendarModel.addedCalendarsUsingBatch.add(calendar);
            }

            @Override
            public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
                System.out.println("Error Message: " + e.getMessage());
            }
        };

        // Create 2 Calendar Entries to insert.
        Calendar entry1 = new Calendar().setSummary("Calendar for Testing 1");
        client.calendars().insert(entry1).queue(batch, callback);

        Calendar entry2 = new Calendar().setSummary("Calendar for Testing 2");
        client.calendars().insert(entry2).queue(batch, callback);

        batch.execute();
    }
}
